package com.crane.common.core.utils;

import com.crane.common.core.exception.AppException;
import com.crane.common.core.exception.BasicErrorCode;


public class SQLFilter {

    /**
     * SQL注入过滤
     *
     * @param str 待验证的字符串
     */
    public static String sqlInject(String str) {
        if (null == str || str.length() == 0) {
            return null;
        }
        //去掉'|"|;|\字符
        str = str.replace("'", "");
        str = str.replace("\"", "");
        str = str.replace(";", "");
        str = str.replace("\\", "");
        //转换成小写
        str = str.toLowerCase();
        //非法字符
        String[] keywords = {"master", "truncate", "insert", "select", "delete", "update", "declare", "alert", "create", "drop"};

        //判断是否包含非法字符
        for (String keyword : keywords) {
            if (str.indexOf(keyword) != -1) {
                throw new AppException(BasicErrorCode.ILLEGAL_CHARACTER, "实体");
            }
        }
        return str;
    }
}
